package minPathSum

func MinPathSum(matrix [][]int) int {
	m, n := len(matrix), len(matrix[0])
	if m <= 0 || n <= 0 {
		return 0
	}
	dp := make([][]int, m)

	for i := 0; i < len(dp); i++ {
		dp[i] = make([]int, n)

	}

	dp[0][0] = matrix[0][0]

	for i := 1; i < m; i++ {
		dp[i][0] = matrix[i][0] + dp[i-1][0]

	}
	for j := 1; j < n; j++ {
		dp[0][j] = matrix[0][j] + dp[0][j-1]

	}
	for i := 1; i < m; i++ {
		for j := 1; j < n; j++ {
			dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + matrix[i][j]
		}

	}

	return dp[m-1][n-1]

}

func min(a, b int) int {

	if a < b {
		return a
	} else {
		return b
	}

}
